
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "data_type.h"
#include "whqx_mesh_service.h"

cJSON *systeminfo() {
    int Socket;
    char RecvData[5120], SendCmd[10240], AuthData[5120];
    struct sockaddr_in addr;
    // 初始化Socket接口
    Socket = socket(AF_INET, SOCK_STREAM, 0);
    if (Socket == -1) {
        perror("socket");
        return -1;
    }
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(meshCfg.ipaddr);
    addr.sin_port = htons(9754);
    if (connect(Socket, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
        perror("connect");
        return -1;
    }
    // 过滤 "console :"
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    // printf("%s\n", RecvData);

    // 认证用户名和密码
    printf("\n--> 认证用户名和密码\n");
    snprintf(SendCmd, sizeof(SendCmd), "auth {\"username\":\"%s\",\"password\":\"%s\"}\n", meshCfg.username, meshCfg.password);
    printf("[Send]: %s", SendCmd);
    if (send(Socket, SendCmd, strlen(SendCmd), 0) < 0) {
        perror("send");
    }
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    snprintf(AuthData, sizeof(AuthData), "%s", RecvData);
    printf("[Recv]: %s", RecvData);

    // 过滤 "console :"
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    // printf("%s\n", RecvData);

    // 获取系统信息
    printf("\n--> 获取系统信息\n");
    snprintf(SendCmd, sizeof(SendCmd), "systeminfo %s", AuthData);
    printf("[Send]: %s", SendCmd);
    if (send(Socket, SendCmd, strlen(SendCmd), 0) < 0) {
        perror("send");
    }
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    printf("[Recv]: %s", RecvData);

    //断开连接
    printf("\n--> 退出\n");
    snprintf(SendCmd, sizeof(SendCmd), "exit\n");
    printf("[Send]: %s", SendCmd);
    if (send(Socket, SendCmd, strlen(SendCmd), 0) < 0) {
        perror("send");
    }

    close(Socket);
    return cJSON_Parse(RecvData);
}

int reboot() {
    int Socket;
    char RecvData[5120], SendCmd[10240], AuthData[5120];
    struct sockaddr_in addr;

    // 初始化Socket接口
    Socket = socket(AF_INET, SOCK_STREAM, 0);
    if (Socket == -1) {
        perror("socket");
        return -1;
    }
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(meshCfg.ipaddr);
    addr.sin_port = htons(9754);
    if (connect(Socket, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
        perror("connect");
        return -1;
    }
    // 过滤 "console :"
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    // printf("%s\n", RecvData);

    // 认证用户名和密码
    printf("\n--> 认证用户名和密码\n");
    snprintf(SendCmd, sizeof(SendCmd), "auth {\"username\":\"%s\",\"password\":\"%s\"}\n", meshCfg.username, meshCfg.password);
    printf("[Send]: %s", SendCmd);
    if (send(Socket, SendCmd, strlen(SendCmd), 0) < 0) {
        perror("send");
    }
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    snprintf(AuthData, sizeof(AuthData), "%s", RecvData);
    printf("[Recv]: %s", RecvData);

    // 过滤 "console :"
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    // printf("%s\n", RecvData);

    // 重启Mesh模块
    cJSON *cjson = cJSON_Parse(AuthData);
    char* secretkey = cJSON_GetObjectItem(cjson,"secretkey")->valuestring;
    printf("\n--> 重启Mesh模块\n");
    snprintf(SendCmd, sizeof(SendCmd), "tool {\"secretkey\":\"%s\",\"action\":\"reboot\"}\n", secretkey);
    printf("[Send]: %s", SendCmd);
    if (send(Socket, SendCmd, strlen(SendCmd), 0) < 0) {
        perror("send");
    }
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    printf("[Recv]: %s", RecvData);
    //断开连接
    printf("\n--> 退出\n");
    snprintf(SendCmd, sizeof(SendCmd), "exit\n");
    printf("[Send]: %s", SendCmd);
    if (send(Socket, SendCmd, strlen(SendCmd), 0) < 0) {
        perror("send");
    }

    close(Socket);
    return 0;
}

int reset() {
    int Socket;
    char RecvData[5120], SendCmd[10240], AuthData[5120];
    struct sockaddr_in addr;

    // 初始化Socket接口
    Socket = socket(AF_INET, SOCK_STREAM, 0);
    if (Socket == -1) {
        perror("socket");
        return -1;
    }
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(meshCfg.ipaddr);
    addr.sin_port = htons(9754);
    if (connect(Socket, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
        perror("connect");
        return -1;
    }
    // 过滤 "console :"
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    // printf("%s\n", RecvData);

    // 认证用户名和密码
    printf("\n--> 认证用户名和密码\n");
    snprintf(SendCmd, sizeof(SendCmd), "auth {\"username\":\"%s\",\"password\":\"%s\"}\n", meshCfg.username, meshCfg.password);
    printf("[Send]: %s", SendCmd);
    if (send(Socket, SendCmd, strlen(SendCmd), 0) < 0) {
        perror("send");
    }
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    snprintf(AuthData, sizeof(AuthData), "%s", RecvData);
    printf("[Recv]: %s", RecvData);

    // 过滤 "console :"
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    // printf("%s\n", RecvData);

    // 重置Mesh模块
    cJSON *cjson = cJSON_Parse(AuthData);
    char* secretkey = cJSON_GetObjectItem(cjson,"secretkey")->valuestring;
    printf("\n--> 重置Mesh模块\n");
    snprintf(SendCmd, sizeof(SendCmd), "tool {\"secretkey\":\"%s\",\"action\":\"reset\"}\n", secretkey);
    printf("[Send]: %s", SendCmd);
    if (send(Socket, SendCmd, strlen(SendCmd), 0) < 0) {
        perror("send");
    }
    memset(RecvData, 0, sizeof(RecvData));
    recv(Socket, RecvData, sizeof(RecvData), 0);
    printf("[Recv]: %s", RecvData);
    //断开连接
    printf("\n--> 退出\n");
    snprintf(SendCmd, sizeof(SendCmd), "exit\n");
    printf("[Send]: %s", SendCmd);
    if (send(Socket, SendCmd, strlen(SendCmd), 0) < 0) {
        perror("send");
    }

    close(Socket);
    return 0;
}